package org.zjx.swing.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class RecognitionView extends JFrame {
    private JTextArea largeDisplayTextArea;
    private JTextArea smallDisplayTextArea;
    private JTextField inputField;
    private JButton previousButton, nextButton, mergeButton, okButton;

    public RecognitionView() {
        setTitle("验字界面");
        setSize(800, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        // 创建并设置主面板
        JPanel mainPanel = createMainPanel();
        add(mainPanel, BorderLayout.CENTER);

        // 创建并设置底部按钮面板
        JPanel buttonPanel = createButtonPanel();
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private JPanel createMainPanel() {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        // 左侧大展示框
        largeDisplayTextArea = new JTextArea();
        largeDisplayTextArea.setLineWrap(false);
        largeDisplayTextArea.setEditable(false);
        JScrollPane largeScrollPane = new JScrollPane(largeDisplayTextArea);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.7;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        mainPanel.add(largeScrollPane, gbc);

        // 右侧容器（包含小展示框和输入框）
        JPanel rightContainer = createRightContainer();
        gbc.gridx = 1;
        gbc.weightx = 0.3;
        mainPanel.add(rightContainer, gbc);

        return mainPanel;
    }

    private JPanel createRightContainer() {
        JPanel rightContainer = new JPanel(new GridLayout(2, 1));

        // 小展示框
        smallDisplayTextArea = new JTextArea();
        smallDisplayTextArea.setLineWrap(true);
        smallDisplayTextArea.setEditable(false);
        smallDisplayTextArea.setFont(new Font("宋体", Font.PLAIN, 24));
        JScrollPane smallScrollPane = new JScrollPane(smallDisplayTextArea);
        rightContainer.add(smallScrollPane);

        // 输入框
        inputField = new JTextField();
        inputField.setFont(new Font("宋体", Font.PLAIN, 24));
        rightContainer.add(inputField);

        return rightContainer;
    }

    private JPanel createButtonPanel() {
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        previousButton = new JButton("上一个");
        nextButton = new JButton("下一个");
        mergeButton = new JButton("合并");
        okButton = new JButton("确认");

        buttonPanel.add(previousButton);
        buttonPanel.add(nextButton);
        buttonPanel.add(mergeButton);
        buttonPanel.add(okButton);

        return buttonPanel;
    }

    // 获取输入框文本
    public String getInputText() {
        return inputField.getText().trim();
    }

    // 设置大展示框文本
    public void setLargeDisplayText(String text) {
        largeDisplayTextArea.setText(text);
    }

    // 设置小展示框文本
    public void appendSmallDisplayText(String text) {
        smallDisplayTextArea.append(text + "\n");
    }

    // 清空输入框
    public void clearInputField() {
        inputField.setText("");
    }

    // 按钮事件监听器
    public void addPreviousButtonListener(ActionListener listener) {
        previousButton.addActionListener(listener);
    }

    public void addNextButtonListener(ActionListener listener) {
        nextButton.addActionListener(listener);
    }

    public void addMergeButtonListener(ActionListener listener) {
        mergeButton.addActionListener(listener);
    }

    public void addOkButtonListener(ActionListener listener) {
        okButton.addActionListener(listener);
    }
}
